﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using SlamDunk.MES.Domain.Entities.ConfigurationContext;

namespace SlamDunk.MES.Repository.EntityConfigurations.ConfigurationContext
{
    /// <summary>
    /// 用户扩展实体映射配置
    /// </summary>
    public class UserExtensionConfig : IEntityTypeConfiguration<UserExtension>
    {
        /// <summary>
        /// 配置
        /// </summary>
        public void Configure(EntityTypeBuilder<UserExtension> builder)
        {
            //配置属性
            builder.HasKey(userExt => userExt.Id).IsClustered(false);
            builder.HasMany(userExt => userExt.RelatedTerminals).WithOne(rel => rel.UserExtension).IsRequired().OnDelete(DeleteBehavior.Cascade);
            builder.HasMany(userExt => userExt.RelatedProcessSegments).WithOne(rel => rel.UserExtension).IsRequired().OnDelete(DeleteBehavior.Cascade);
            builder.HasMany(userExt => userExt.RelatedStorageLocations).WithOne(rel => rel.UserExtension).IsRequired().OnDelete(DeleteBehavior.Cascade);
            builder.Property(userExt => userExt.Number).HasMaxLength(20);

            //配置索引
            builder.HasIndex(userExt => userExt.AddedTime).IsUnique(false).IsClustered();
            builder.HasIndex(userExt => userExt.Number).IsUnique();
        }
    }
}
